;layui.define(function(e) {
    layui.use(['table','layer','form','laydate'], function(){
        var $ = layui.$
            ,layer = layui.layer
            ,form = layui.form
            ,table = layui.table;
        parentDom=$("#car_table");
        //展示已知数据
        table.render({
            elem: '#idTest'
            ,cols: [[ //标题栏
                {field: 'brand', title: '品牌'}
                ,{field: 'carSeries', title: '车系'}
                ,{field: 'carType', title: '车型', minWidth: 120}
                ,{field: 'guidePrice', title: '指导价(元)', sort: true}
                ,{field: 'purchasePrice', title: '采购价(元)', sort: true}
                ,{field: 'closePrice', title: '批售价(元)', sort: true}
                ,{field: 'payment', title: '支付方式'}
                ,{field: 'area', title: '销售区域'}
                ,{field: 'arrivalPeriod', title: '到货周期(天)',sort: true}
                ,{field: 'minimum', title: '起订数量(辆)',sort: true}
                ,{field: 'inventory', title: '库存数量(辆)',sort: true}
                ,{field: 'tool',title: '操作',fixed: 'right', minWidth:158, align:'center', toolbar: '#barDemo'}
            ]]
            ,height: 'full-300'
            ,cellMinWidth: 120
            ,data: [{
                "brand": "10001"
                ,"carSeries": "杜甫"
                ,"carType": "xianxin@layui.com"
                ,"guidePrice": "男"
                ,"purchasePrice": "浙江杭州"
                ,"closePrice": "人生恰似一场修行"
                ,"payment": "116"
                ,"area": "192.168.0.8"
                ,"arrivalPeriod": "108"
                ,"minimum": "2016-10-14"
                ,"inventory": "192.168.0.8"
            }]
            ,page: true //是否显示分页
        });
        //监听工具条
        table.on('tool(carInquireTable)', function(obj){
            var data = obj.data;
            if(obj.event === 'detail'){
                var index = layer.open({
                    type: 2
                    ,id: (new Date()).valueOf()
                    ,title: '详情'
                    ,content: '../car_source/detail.html'
                    ,area: ['500px', '500px']
                    ,btn: ['确定', '取消']
                    ,maxmin: true
                    ,success: function(layero, index){
                        var body = layer.getChildFrame('body', index);
                        var iframeWin = window[layero.find('iframe')[0]['name']]; //得到iframe页的窗口对象，执行iframe页的方法：iframeWin.method();
//                        console.log(body.html()) //得到iframe页的body内容
                        var paramId=body.find("input[name='id']");
                        paramId.val(111);
                    }
                    ,yes: function (layero, index) {
                        layer.alert("确定按钮");
                        return false //开启该代码可禁止点击该按钮关闭
                    }
                    ,btn2: function (layero, index) {
                        layer.alert("取消按钮")
                        //return false 开启该代码可禁止点击该按钮关闭
                    }
                });
                layer.full(index);
            }
        });
        form.render();
        //下意向单
        $("#wishList").click(function () {
            var index = layer.open({
                type: 2
                ,id: (new Date()).valueOf()
                ,title: '下意向单'
                ,content: 'wishList.html'
                ,area: ['500px', '500px']
                ,maxmin: true
            });
            layer.full(index);
        });
        var getId = (function () {
            var i = 0;
            return function () {
                return i++;
            };
        })();

        $("#enter_purchased_vehicles").click(function () {
            var index = layer.open({
                type: 2
                ,id: (new Date()).valueOf()
                ,title: '编辑'
                ,content: 'edit.html'
                ,area: ['500px', '500px']
                ,btn:['确认','取消']
                ,maxmin: true
                ,success: function(layero, index){
                    var body = layer.getChildFrame('body', index);
                    var iframeWin = window[layero.find('iframe')[0]['name']]; //得到iframe页的窗口对象，执行iframe页的方法：iframeWin.method();
                    // console.log(body.html()) //得到iframe页的body内容
                },
                yes: function (index,layero) {
                    var j = getId();
                    alert(j)
                    var body = layer.getChildFrame('body', index);
                    var carTableLength = parseInt(body.find("input[name=car_number]").val());
                    var carTableHtml='<div class="car_table">\n' +
                        '                        <div class="layui-btn-container">\n' +
                        '                            <a href="javascript:;" class="layui-btn layui-btn-sm layui-bg-blue carEdit" id="edit'+index+'">编辑</a>\n' +
                        '                            <a href="javascript:;" class="layui-btn layui-btn-sm layui-bg-red carDelete"  id="delete'+index+'">删除</a>\n' +
                        '                        </div>\n' +
                        '                        <input type="hidden" id="table'+ index +'" name="table'+ j +'" value=""/>' +
                        '                        <table class="layui-table table'+ index+'">\n' +
                        '                            <thead>\n' +
                        '                            <tr>\n' +
                        '                                <th width="25%">品牌</th>\n' +
                        '                                <th width="25%">车系</th>\n' +
                        '                                <th width="25%">车型</th>\n' +
                        '                                <th width="25%">外观/内饰颜色</th>\n' +
                        '                            </tr>\n' +
                        '                            </thead>\n' +
                        '                            <tbody>\n' +
                        '                            </tbody>\n' +
                        '                        </table>\n' +
                        '                    </div>'
                    var brand = body.find('select[name=car_brand_series]').val();
                    var car_brand_series = body.find('select[name=car_brand_series]').val();
                    var color_out = body.find('select[name=color_out]').val();
                    $("#car_table").append(carTableHtml);
                    var tbody = $("#car_table .layui-table tbody");
                    var tbodyLength = $("#car_table .layui-table tbody").length;
                    tbody.eq(tbodyLength-1).empty();
                    for(var i=0;i<carTableLength;i++){
                        j=getId();
                        tbody.eq(tbodyLength-1).append('<tr>\n' +
                            '                                <td><input type="text" class="brand" name="brand'+ j +'"  value="'+brand+'" readonly/></td>\n' +
                            '                                <td><input type="text" class="carSeries"  name="carSeries'+ j +'"  value="'+car_brand_series+'" readonly/></td>\n' +
                            '                                <td><input type="text" class="carType"  name="carType'+ j +'" value="'+car_brand_series+'" readonly/></td>\n' +
                            '                                <td><input type="text" class="carColor" name="carColor'+ j +'" value="'+color_out+'" readonly/></td>\n' +
                            '                            </tr>')
                    }
                    var table_value = $('.table'+ index+'').find(".brand").eq(0).val()+","+$('.table'+ index+'').find(".carColor").eq(0).val();
                    $('#table'+ index +'').val(table_value);
                    $("#edit"+index+"").click(function () {
                        carEdit($(this))
                    })
                    $("#delete"+index+"").click(function () {
                        carDelete($(this));
                        if($("#car_table").children().length == 0){
                            $("input[name=payStyle]").attr("disabled",true).attr("checked",false);
                            form.render();
                        }
                    });
                    $("input[name=payStyle]").removeAttr("disabled");
                    form.render();
                    layer.close(index);
                }
            });
            layer.full(index);
        })
        // form.on('radio(payment)',function (data) {
        //     var table = $("#car_table table");
        //     table.each(function (e) {
        //         var tableName = $(this).attr("name");
        //         $(this).attr("name",tableName+"["+e+"]")
        //     })
        //     var tr = $("#car_table tbody tr");
        //     tr.each(function (e) {
        //         tr.eq(e).find("td").each(function (q) {
        //             var tdName = $(this).attr("name");
        //             $(this).attr("name",tdName+"["+q+"]")
        //         })
        //     })
        //     if(data.value == "1"){
        //         var obj = {};
        //         var formArray = $("#carInquire_form").serializeArray();
        //         $.each(formArray, function () {
        //             if (obj[this.name] !== undefined) {
        //                 if (!obj[this.name].push) {
        //                     obj[this.name] = [obj[this.name]];
        //                 }
        //                 obj[this.name].push(this.value || '');
        //             } else {
        //                 obj[this.name] = this.value || '';
        //             }
        //         });
        //         // var formdatas = new FormData("#carInquire_form");
        //         // formdatas.append("data",obj)
        //         console.log(obj);
        //     }
        // })


        //编辑弹框
        function carEdit(obj) {
            var index = layer.open({
                type: 2
                ,id: (new Date()).valueOf()
                ,title: '编辑'
                ,content: 'edit.html'
                ,area: ['500px', '500px']
                ,maxmin: true
                ,btn:['确定','取消']
                ,success: function(layero,index){
                    var body = layer.getChildFrame('body', index);
                    var iframeWin = window[layero.find('iframe')[0]['name']]; //得到iframe页的窗口对象，执行iframe页的方法：iframeWin.method();
//                        console.log(body.html()) //得到iframe页的body内容
                    var brand = obj.parent().parent().find(".brand").eq(0).val();
                    var carColor = obj.parent().parent().find(".carColor").eq(0).val();
                    body.find("select[name=car_brand_series]").val(brand);
                    body.find("select[name=color_out]").val(carColor);
                    body.find("select[name=color_in]").val(carColor);
                }
                ,yes:function (index,layero) {
                    var j = getId()
                    var body = layer.getChildFrame('body', index);
                    var carTableLength = parseInt(body.find("input[name=car_number]").val());
                    obj.parent().parent().find("tbody").html("");
                    for(var i=0;i<carTableLength;i++){
                        j = getId();
                        obj.parent().parent().find("tbody").append('<tr>\n' +
                            '                                <td><input type="text" class="brand" name="brand'+ j +'"  value="'+body.find("select[name=car_brand_series]").val()+'" readonly/></td>\n' +
                            '                                <td><input type="text" class="carSeries"  name="carSeries'+ j +'"  value="'+body.find("select[name=car_brand_series]").val()+'" readonly/></td>\n' +
                            '                                <td><input type="text" class="carType"  name="carType'+ j +'" value="'+body.find("select[name=car_brand_series]").val()+'" readonly/></td>\n' +
                            '                                <td><input type="text" class="carColor" name="carColor'+ j +'" value="'+body.find("select[name=color_out]").val()+'" readonly/></td>\n' +
                            '                            </tr>')
                    }
                    layer.close(index);
                }
            });
            layer.full(index);
        }
        function carDelete(obj) {
            obj.parent().parent().remove()
        }
    });
    //输出模块
    e("carInquire", {})
});